package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Letter;
import cn.wolfcode.trip.base.mapper.LetterMapper;
import cn.wolfcode.trip.base.query.LetterQueryObject;
import cn.wolfcode.trip.base.query.SystemMessageQueryObject;
import cn.wolfcode.trip.base.service.ILetterService;
import cn.wolfcode.trip.base.util.TimeUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LetterServiceImpl implements ILetterService {
    @Autowired
    private LetterMapper letterMapper;


    //将状态修改为已读
    public void updateState(Long sendId, Long acceptId, Integer state) {
        letterMapper.updateState(sendId,acceptId,state);
    }

    @Override
    public List<Map> queryBySendIdOrAcceptId(Long sendId, Long acceptId) {
        List<Map> list =letterMapper.queryBySendIdOrAcceptId(sendId,acceptId);
        for (Map map : list) {
            Date createTime = (Date)map.get("createtime");
            //格式化
            String Object = TimeUtil.DateformatTime(createTime);
            map.put("formatTime",Object);
        }
        return list;
    }
    //获得最新的 收到的信息并且修改 状态
    public List<Map> changeStateAndGetLetters(Long sendId, Long acceptId) {
        List<Map> list = letterMapper.getLetterUnread(sendId,acceptId);
        if(list.size() > 0) {
            for (Map map : list) {
                Date createTime = (Date) map.get("createtime");
                //格式化
                String Object = TimeUtil.DateformatTime(createTime);
                map.put("formatTime", Object);
            }
            //查出来就修改他们的状态为已读
            letterMapper.updateState(sendId, acceptId, Letter.STATE_READED);
        }
        return list;
    }

    public Map<Object, Object> sendLetter(Long sendId, Long acceptId, String content) {
        Date date = new Date();
        Map<Object, Object> map = new HashMap();
        letterMapper.insert(sendId,acceptId,content,date);
        map.put("createtime",date);
        map.put("formatTime",TimeUtil.DateformatTime(date));
        return map;
    }

    public PageInfo queryUnreadLetterByUserId(LetterQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), "m.createTime desc");
        List<Map> list = letterMapper.query(qo);
        for (Map map : list) {
            Date createTime = (Date)map.get("createTime");
            //格式化
            String Object = TimeUtil.DateformatTime(createTime);
            map.put("createTime",Object);
        }
        return new PageInfo(list);
    }

    @Override
    public void updataLetterStateByUserId(Long id) {

    }


    public Object countUnReadletters(LetterQueryObject qo) {
        //判断是否 查出 为null
        Integer countUnReadletters = letterMapper.countUnReadSystemMessages(qo);
        if(countUnReadletters == null){
            return 0;
        }else {
            return  countUnReadletters;
        }
    }


}
